﻿
Public Class NDateTimePicker

    Dim previousDateTime As DateTime = DateTime.Now


    Public Property Value As DateTime
        Get
            Return Me.Actual_DateTimePicker.Value
        End Get
        Set(value As DateTime)
            If value = DateTime.MinValue Then
                Me.previousDateTime = Windows.Forms.DateTimePicker.MinimumDateTime
                Me.EnableDisable_CheckBox.Checked = False
                Me.EnableDisableStateUpdate()
            Else
                Me.previousDateTime = value
                If Me.EnableDisable_CheckBox.Checked = False Then
                    Me.EnableDisable_CheckBox.Checked = True
                    Me.EnableDisableStateUpdate()
                Else
                    Me.Actual_DateTimePicker.Value = Me.previousDateTime
                End If
            End If
        End Set
    End Property

    Protected Overrides Sub OnFontChanged(e As System.EventArgs)
        MyBase.OnFontChanged(e)
        Me.Actual_DateTimePicker.Font = Me.Font
        ProportionSizeAndRelocateControls()
    End Sub

    Protected Overrides Sub OnResize(e As System.EventArgs)
        MyBase.OnResize(e)
        ProportionSizeAndRelocateControls()
    End Sub

    Private Sub ProportionSizeAndRelocateControls()
        Me.Size = New Drawing.Size(Me.Size.Width, Me.Actual_DateTimePicker.Size.Height)
        Me.EnableDisable_CheckBox.Location = New Drawing.Point(0, 0)
        Me.EnableDisable_CheckBox.Size = New Drawing.Size(13, Me.Size.Height)
        Me.Actual_DateTimePicker.Location = New Drawing.Point(15, 0)
        Me.Actual_DateTimePicker.Size = New Drawing.Size(Me.Size.Width - Me.EnableDisable_CheckBox.Size.Width, Me.Actual_DateTimePicker.Size.Height)
    End Sub

    Private Sub EnableDisable_CheckBox_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles EnableDisable_CheckBox.CheckedChanged
        If EnableDisable_CheckBox.Checked = True And Me.previousDateTime = Windows.Forms.DateTimePicker.MinimumDateTime Then Me.previousDateTime = DateTime.Now
        EnableDisableStateUpdate()
    End Sub

    Private Sub EnableDisableStateUpdate()
        If EnableDisable_CheckBox.Checked Then
            Me.Actual_DateTimePicker.Value = Me.previousDateTime
        Else
            Me.previousDateTime = Me.Actual_DateTimePicker.Value
            Me.Actual_DateTimePicker.Value = Windows.Forms.DateTimePicker.MinimumDateTime
        End If
        Me.Actual_DateTimePicker.Enabled = EnableDisable_CheckBox.Checked
    End Sub

    'Used to restrict one of the size horizons
    'Protected Overrides Sub SetBoundsCore(x As Integer, y As Integer, width As Integer, height As Integer, specified As System.Windows.Forms.BoundsSpecified)
    '    MyBase.SetBoundsCore(x, y, width, Me.Actual_DateTimePicker.Height, specified)
    'End Sub

    Private Sub NDateTimePicker_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Value = DateTime.Now
        EnableDisableStateUpdate()
    End Sub

    Private Sub Actual_DateTimePicker_ValueChanged(sender As Object, e As System.EventArgs) Handles Actual_DateTimePicker.ValueChanged
        If Me.Actual_DateTimePicker.Value <> Windows.Forms.DateTimePicker.MinimumDateTime Then
            'Me.previousDateTime = Me.Actual_DateTimePicker.Value
        End If
    End Sub

End Class
